package com.inspur.edp.common.expr.visitor.array;


import com.inspur.edp.common.expr.ast.CompileUnitParser;
import com.inspur.edp.common.expr.parser.ExpressAstParser;
import com.inspur.edp.common.expr.visitor.JavaVisitor;
import com.inspur.edp.common.expr.visitor.VisitResult;
import com.inspur.edp.common.type.Type;
import com.inspur.edp.common.type.TypeRefer;

/**
 * @author lizhaorui
 * @description TODO
 * @date 2024/9/12 10:50
 */
public class ArrayAccessManager {

    public static VisitResult visitGetter(JavaVisitor visitor, CompileUnitParser.ExpressContext objectExpr, CompileUnitParser.ExpressContext indexExpress) {
        TypeRefer objectType = ExpressAstParser.getOriginalType(objectExpr, visitor);
        Type type = visitor.getType(objectType);
        String kind = type.getArrayAccessKind();
        return ArrayAccessFactory.getSpi(kind).visitGetter(
                visitor,
                visitor,
                objectExpr,
                indexExpress
        );
    }

    public static VisitResult visitSetter(JavaVisitor visitor,
                                              CompileUnitParser.ExpressContext objectExpr,
                                              CompileUnitParser.ExpressContext indexExpress,
                                              CompileUnitParser.ExpressContext valueExpress) {
        TypeRefer objectType = ExpressAstParser.getOriginalType(objectExpr, visitor);
        Type type = visitor.getType(objectType);
        String kind = type.getArrayAccessKind();
        return ArrayAccessFactory.getSpi(kind).visitSetter(
                visitor,
                visitor,
                objectExpr,
                indexExpress,
                valueExpress
        );
    }

}
